package com.jc.subject.domin.handler.subject;

import com.jc.subject.common.enums.ResultCodeEnum;
import com.jc.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 题目类型工厂
 * @Author: ASL_ly
 * @Package: com.jc.subject.domin.handler.subject
 * @Project: jc-club
 * @Date: 2024/10/23  20:42
 */
@Component
public class SubjectTypeFactory implements InitializingBean {

    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlerList;

    private Map<SubjectInfoTypeEnum,SubjectTypeHandler> map= new HashMap();

    public SubjectTypeHandler getHandler(int subjectType){
        SubjectInfoTypeEnum subjectInfoTypeEnum  = SubjectInfoTypeEnum.getByCode(subjectType);
        return map.get(subjectInfoTypeEnum);
    }

    /**
     * 通过实现 InitializingBean 接口，在 Spring 容器初始化时，
     * 调用 afterPropertiesSet() 方法，将 subjectTypeHandlerList
     * 中的处理器与具体的题目类型进行映射，存储在 map 中。
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlerList) {
            map.put(subjectTypeHandler.getHandlerType(),subjectTypeHandler);
        }
    }
}
